package seeders

import (
	"fmt"

	"gorm.io/gorm"

	"github.com/runningwater/gohub/database/factories"
	"github.com/runningwater/gohub/pkg/console"
	"github.com/runningwater/gohub/pkg/logger"
	"github.com/runningwater/gohub/pkg/seed"
)

func init() {
	// 添加 Seeder
	seed.Add("UsersTableSeeder", usersTableSeederFun)
}

func usersTableSeederFun(db *gorm.DB) {
	// 创建 10 个用户对象
	users := factories.MakeUsers(10)

	// 批量插入到数据库
	result := db.Table("users").Create(&users)

	if err := result.Error; err != nil {
		logger.LogIf(err)
		return
	}

	// 打印成功信息
	console.Success(fmt.Sprintf("Table [%v] %v rows seeded", result.Statement.Table, result.RowsAffected))
}
